﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Drawing;

namespace WanPiJie.Img
{
    /// <summary>
    /// 图片下载器
    /// </summary>
    public class ImageDownloader
    {
        private static HttpLoader httpLoader;

        static ImageDownloader()
        {
            httpLoader = new HttpLoader();
        }

        /// <summary>
        /// 下载图片
        /// </summary>
        /// <param name="filePath">图片文件的保存地址</param>
        /// <param name="url">图片的下载地址</param>
        /// <returns>图片文件路径</returns>
        public static void Download(string filePath, string url)
        {
            MemoryStream ms = httpLoader.SaveResponseToMemory(url);
            if (httpLoader.Response.ContentType != "image/jpeg " && httpLoader.Response.ContentLength != 0)
            {
                Image originalImage = Image.FromStream(ms);
                originalImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                ms.Close();
                ms.Dispose();
                originalImage.Dispose();
                originalImage = null;
            }
            ms = null;
        }
    }
}
